//
//  Model.m
//  iOS-Interview
//
//  Created by Gechanghang on 2018/5/15.
//  Copyright © 2018年 CHANG HANG GE. All rights reserved.
//

#import "Model.h"
#import "PeopleModel.h"
#import <objc/runtime.h>
@implementation Model
@synthesize name;
void eat(id self,SEL _cmd) {
    NSLog(@"eat");
}

//动态添加方法
+(BOOL)resolveInstanceMethod:(SEL)sel {
    NSString *selectorString = NSStringFromSelector(sel);
    NSLog(@"resolveInstanceMethod %@",selectorString);
    if ([selectorString isEqualToString:@"eat"]) {
        class_addMethod(self, sel, (IMP)eat, "v@:");
    }
    return [super resolveInstanceMethod:sel];
}
//备援接受者
- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSString *selectorString = NSStringFromSelector(aSelector);
    NSLog(@"forwardingTargetForSelector %@",selectorString);
    PeopleModel *people = [PeopleModel new];
    if ([people respondsToSelector:aSelector]) {
        // 交给别的对象处理
        return people;
    }
    return [self forwardingTargetForSelector:aSelector];
}
//完整消息转发
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSString *selectorString = NSStringFromSelector([anInvocation selector]);
    NSLog(@"forwardInvocation %@",selectorString);
    if ([selectorString isEqualToString:@"code"]) {
        PeopleModel *people = [PeopleModel new];
        [anInvocation invokeWithTarget:people];
    }
}
@end
